import ReactDOM from 'react-dom/client';
import { ConfigProvider } from 'antd';
import axios from 'axios';
import zhCN from 'antd/es/locale/zh_CN';
import Routers from './router';
import { BaseURL } from './config';

axios.defaults.baseURL = BaseURL;

// axios 请求拦截器处理请求数据
axios.interceptors.request.use(config => {
  const token = localStorage.getItem('token');
  if (token) {
    config.headers['Authorization'] = 'Bearer ' + token; // 留意这里的 Authorization
  }
  
  return config;
});

axios.interceptors.response.use(res => {
  return res;
}, err => {
  if (err.response.status === 401) {
    window.location.href = '/#/login';
  }
  return Promise.reject(err);
});

ReactDOM.createRoot(document.getElementById('root')!).render(
  <ConfigProvider locale={zhCN}>
    <Routers />
  </ConfigProvider>
);
